# coding: UTF-8

import io
import sys
import os
import re
import json
import copy
import argparse

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

from YTSTools import file
from YTSTools import convert
from YTSTools import font_format
import gBlog

def UserArgs():
    try:
        parser = argparse.ArgumentParser(description='Python Generate Blog (Python 脚本生成博客项目)')
        args = parser.parse_args()
        return {
            'program_address': os.path.split(os.path.realpath(__file__))[0],
        }
    except Exception as e:
        print(e)
        return None

def develop_config():
    isWin = convert.is_window_system()
    index = 0 if isWin else 1
    return file.read_program_config('config.json', {
        "ignores": [
            "\\.git$",
            "\\.gitignore$",
            "LICENSE$",
        ],
        "projects": [
            {
                "source": [
                    "D:\\Work\\YTS.ZRQ\\-ArticleNotes",
                    "/var/work/YTS.ZRQ/-ArticleNotes"
                    ][index],
                "target": [
                    "D:\\wwwroot\\YTS.ZRQ\\-ArticleNotes",
                    "/var/wwwroot/YTS.ZRQ/-ArticleNotes"
                    ][index],
                # "template": "./templates/default",
                "ignores": [
                    "language$"
                ],
                "info": {
                    "site": {
                        "root": "/-ArticleNotes/",
                        "icon": "/-ArticleNotes/images/favicon.ico",
                        "title": "YTS.ZRQ的学习笔记",
                        "keyworks": "YTS.ZRQ, 学习, 笔记",
                        "description": "笔记备注内容",
                    },
                    "articles": [],
                },
            }
        ],
    })

def main(userArgs):
    # 读取 json 配置文件
    config = develop_config()
    projects = config.get('projects', [])
    for pro in projects:
        blog = gBlog.Blog(userArgs, config, pro)
        blog.Generate()

if __name__ == '__main__':
    userArgs = UserArgs()
    if userArgs:
        main(userArgs)
